<?php
class admin_module_artist
{
    private $template;
    private $util;
    private $db;
    private $content;
    private $module;
    private $id;
    
    public function __construct(){ 
        require_once("../code/db.class.php");
        $this->util = new util();
        $this->db = new db();
        
        
    }
    
    public function autorun(){
         $act = isset($_GET['act'])?$_GET['act']:'list';
         $this->id = isset($_GET['id'])?intval($_GET['id']):0;
         switch($act){
            case 'list':
                $this->list_artist();
                break;
            case 'create':
                $this->create_artist();
                break;
            case 'docreate':
                $this->do_create_artist();
                break;
            case 'update';
                $this->update_artist();
                break;
            case 'doupdate';
                $this->do_update_artist();
                break;
            case 'delete':
                $this->do_delete_artist();
                break;
            case 'status':
                $this->do_change_status();
                break;
            default:
                $this->list_artist();
                break;
         }     
    } 
    //List artist
    private function list_artist(){
        //Load data
        //$artist = array('1'=>array('id'=>1,'name'=>'Nhạc nhẹ'),'2'=>array('id'=>2,'name'=>'Nhạc quốc tế'),'3'=>array('id'=>3,'name'=>'Nhạc trẻ'));
        $filter = isset($_GET['filter'])?strval($_GET['filter']):'all'; 
        switch ($filter)
        {
            case 'all':
                $str = "SELECT * FROM artist"; 
                break;
            case 'online':
                $str = "SELECT * FROM artist where status=1"; 
                break;
            case 'offline':
                $str = "SELECT * FROM artist where status=0"; 
                break;
            default:
                $str = "SELECT * FROM artist"; 
                break;
                
        }
        $this->db->openconnection();
        $data = $this->db->run_query($str);
        $this->db->closeconnection();
        //Send data to template
        $this->module = 'list_table';
        $this->util->send_var('module',$this->module);
        $this->util->send_var('data',$data);
        $this->util->generate_page(MUSIC_ADMIN_TEMPLATES.'/artist/artist.tpl');  
    } 
    
    //Load template create new artist
    private function create_artist(){
        $editor = $this->util->create_editor('artist_des');
        //-----------------------------------
        $this->module = 'create_form';
        $this->util->send_var('module',$this->module);
        $this->util->send_var('editor',$editor);
        $this->util->generate_page(MUSIC_ADMIN_TEMPLATES.'/artist/artist.tpl'); 
    }
    //Do Create new artist
    private function do_create_artist(){
        //take input
        $artist_name = isset($_POST['artist_name'])?$_POST['artist_name']:'';
        //$artist_avatar = isset($_POST['artist_avatar'])?$_POST['artist_avatar']:0; 
        $artist_des = isset($_POST['artist_des'])?strval($_POST['artist_des']):'';
        $artist_status = isset($_POST['artist_status'])?1:0;
        //Upload file
        $artist_avatar = $this->util->uploader('artist_avatar','avatar/artist/');
        if(($artist_avatar['error']==FILE_EXISTS) || (($artist_avatar['error']==0))){
            $avatar = $artist_avatar['name'];    
        }
        else
        {
            $avatar = '';    
        }
        //Insert database
        $str = "INSERT INTO artist(name,avatar,des,status) VALUES ('$artist_name','$avatar', '$artist_des', '$artist_status')";        
        $this->db->openconnection();
        $this->db->run_query($str);
        $this->db->closeconnection();
        //Oke
        $this->list_artist();        
    }
    //Load template to update artist
    private function update_artist(){
         if($this->id==0){
            //Lỗi: không có giá trị 0 trong csdl
            exit();    
        }
        $this->db->openconnection();
        $data = $this->db->run_select_by_id('artist',$this->id);
        $this->db->closeconnection();
        $editor = $this->util->create_editor('artist_des',$data['des']);
        //Send data to template
        $this->module = 'update_form';
        $this->util->send_var('module',$this->module);
        $this->util->send_var('editor',$editor);
        $this->util->send_var('data',$data);
        $this->util->generate_page(MUSIC_ADMIN_TEMPLATES.'/artist/artist.tpl');    
    }
    //Do update artist
    private function do_update_artist(){
        if($this->id==0){
            //Lỗi: không có giá trị 0 trong csdl
            exit();    
        }
        //take input
        $artist_name = isset($_POST['artist_name'])?$_POST['artist_name']:'';
        //$artist_avatar = isset($_POST['artist_avatar'])?$_POST['artist_avatar']:0; 
        $artist_des = isset($_POST['artist_des'])?strval($_POST['artist_des']):'';
        $artist_status = isset($_POST['artist_status'])?1:0;
        //Upload file
        $artist_avatar = $this->util->uploader('artist_avatar','avatar/artist/');
        if(($artist_avatar['error']==FILE_EXISTS) || (($artist_avatar['error']==0))){
            $avatar = $artist_avatar['name']; 
            $str = "UPDATE artist SET name = '$artist_name', avatar = '$avatar', des = '$artist_des', status = '$artist_status' WHERE id= $this->id";    
        }
        else
        {
            $avatar = '';
            $str = "UPDATE artist SET name = '$artist_name', des = '$artist_des', status = '$artist_status' WHERE id= $this->id";     
        }
        //Insert database
             
        $this->db->openconnection();
        $this->db->run_query($str);
        $this->db->closeconnection();
        //Oke
        $this->list_artist();           
    }
    
    //Do delete artist
    private function do_delete_artist(){
         if($this->id==0){
            //Lỗi: không có giá trị 0 trong csdl
            exit();    
        }
        $str = "DELETE FROM artist WHERE id=$this->id";
        $this->db->openconnection();
        $this->db->run_query($str);
        $this->db->closeconnection();
        //Oke
        $this->list_artist();     
    }
    //Do change status
    private function do_change_status(){
        if($this->id==0){
            //Lỗi: không có giá trị 0 trong csdl
            exit();    
        }
        $status =  isset($_GET['status'])?$_GET['status']:'error';
         
        if($status=='error'){
            print "Lỗi: không có giá trị 0 trong csdl";
            exit();    
        }
        print $status;
        //Only update when status = 1 or 0
        if($status==0 || $status==1){
            $str = "UPDATE artist SET status = $status WHERE id=$this->id";
            $this->db->openconnection();
            $this->db->run_query($str);
            $this->db->closeconnection();      
        }
        //Oke
        $this->list_artist();     
    }
}
?>
